// 一维数组转树形数组
export const arrayToTree = (array) => {
    const map = {}
    const tree = []
    // 将一维数组转换为映射
    array.forEach((item) => {
        map[item.id] = { ...item, children: [] }
    })
    // 构建树形结构
    array.forEach((item) => {
        if (item.pId) {
            map[item.pId].children.push(map[item.id])
        } else {
            tree.push(map[item.id])
        }
    })
    return tree
}
// 树形数组转一维数组
export const treeToArray = (tree) => {
    const result = []

    function traverse(node) {
        result.push({ ...node })
        if (node.children) {
            node.children.forEach((child) => traverse(child))
        }
    }

    tree.forEach((node) => traverse(node))
    return result
}
